﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Windows.Threading;
using ScrumTable.Common;
using ScrumTable.Common.Logging;
using ScrumTable.Common.Threading;

#endregion

namespace ScrumTable.UI.View.Util
{
    /// <summary>
    /// Represents the data synchronisation dispatcher. The invoke method
    /// will be called in order to synchronize back into the windows message
    /// queue of WPF, which created and is holding the data layer.
    /// </summary>
    public class SyncDispatcher : IDataSyncDispatcher
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly Dispatcher _dispatcherLoop;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
      

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="SyncDispatcher"/> class.
        /// </summary>
        /// <param name="dispatcherLoop">The dispatcher </param>
        public SyncDispatcher(Dispatcher dispatcherLoop)
        {
            PreCondition.AssertNotNull(dispatcherLoop, "dispatcherLoop");

            _dispatcherLoop = dispatcherLoop;
        } 

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Each call, which may be fired in an asynchronous context, will
        /// be synchronized using this dispatcher call.
        /// </summary>
        /// <param name="action">Specifies the action to execute in the main message loop.</param>
        public void BeginInvoke(Action action)
        {
            Action toExecute = () =>
                                   {
                                       try
                                       {
                                           action();
                                       }
                                       catch (Exception e)
                                       {
                                           Logger<SyncDispatcher>.Error(e);
                                           throw;
                                       }
                                   };

            if (_dispatcherLoop.CheckAccess())
                toExecute();
            else
                _dispatcherLoop.BeginInvoke(DispatcherPriority.Background, toExecute);
        }

        /// <summary>
        /// Each call, which may be fired in an asynchronous context, will
        /// be synchronized using this dispatcher call.
        /// </summary>
        /// <param name="action">Specifies the action to execute in the main message loop.</param>
        public void Invoke(Action action)
        {
            Action toExecute = () =>
            {
                try
                {
                    action();
                }
                catch (Exception e)
                {
                    Logger<SyncDispatcher>.Error(e);
                    throw;
                }
            };

            if (_dispatcherLoop.CheckAccess())
                toExecute();
            else
                _dispatcherLoop.Invoke(DispatcherPriority.Background, toExecute);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}